package org.overmind.ieat.server.factory;

import org.overmind.ieat.server.model.ExceptionInfo;
import org.overmind.ieat.server.logic.locale.LocaleManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;

/**
 * @author eugene.karanda
 */
@Service //TODO maybe component
public class ExceptionInfoFactoryImpl implements ExceptionInfoFactory {

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private LocaleManager localeManager;

    @Override
    public ExceptionInfo createExceptionInfo(HttpServletRequest request, String messageName, Object... messageArguments) {
        ArrayList<Locale> userLocales = Collections.list(request.getLocales());
        Locale locale = localeManager.getPreferredLocale(userLocales);
        String message = messageSource.getMessage(messageName, messageArguments, locale);//TODO default message
        String requiredURL = request.getRequestURL().toString();

        return new ExceptionInfo(requiredURL, message);
    }
}
